﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Vanfa.Queries.Sql
{
    /// <summary>
    /// 数据库实现
    /// </summary>
    public abstract class TDbImpl<TDb> : DbServer where TDb : class,new()
    {
        public static TDb New { get; private set; }

        static TDbImpl()
        {
            New = new TDb();
        }

        /// <summary>
        /// 将DataReader数据转化为实体对象
        /// </summary>
        /// <typeparam name="TDbEnt"></typeparam>
        /// <param name="read"></param>
        /// <returns></returns>
        protected TDbEnt ConvertTo<TDbEnt>(IDataReader read) where TDbEnt : class,new()
        {
            TDbEnt ent = new TDbEnt();
            var t = typeof(TDbEnt);
            foreach (var p in t.GetProperties())
            {
                if (read.GetOrdinal(p.Name) >= 0)
                    p.SetValue(ent, read[p.Name], null);
            }
            return ent;
        }
    }
}
